#ifdef MU_LICENSING
#include "include/licenseagreement.h"
#include "ui_licenseagreement.h"

#include <QScrollBar>
#include <QDebug>
#include <QMessageBox>
#include <QKeyEvent>

LicenseAgreement::LicenseAgreement(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::LicenseAgreement)
{
	ui->setupUi(this);
	ui->proceed->hide();
	ui->agree->hide();
	ui->disagree->hide();

	m_userAgreed = false;

	m_pScrollBar = ui->agreement->verticalScrollBar();

	m_timer.start();

	connect(m_pScrollBar, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
}

LicenseAgreement::~LicenseAgreement()
{
	delete ui;
}

bool LicenseAgreement::getUserResponse()
{
	exec();
	return m_userAgreed;
}

void LicenseAgreement::on_agree_clicked()
{
	int timeTaken = m_timer.elapsed()/1000;

	// pop dialog if user didn't read for more than 30 seconds
	// allow developers to skip this by pressing "d" before
	// prematures hitting 'Agree'
	if (timeTaken < 30 && m_pressedKey != "d")
	{
		QString message = tr("Time taken: %1 seconds\nPlease read the EULA").arg(timeTaken);

		QMessageBox::warning(this, tr("EULA Warning"),
									  message,
									   QMessageBox::Ok);
		//m_timer = QElapsedTimer();
		//m_timer.start();  //this made the license dialog really anoying for user had to wait 30 sec after any click on agree check box
		m_pScrollBar->setValue(m_pScrollBar->minimum());
		ui->agree->setChecked(false);
		return;
	}

	ui->agree->isChecked() ? ui->proceed->show() : ui->proceed->hide();
}

void LicenseAgreement::keyPressEvent ( QKeyEvent * event )
{
	m_pressedKey = event->text();
}


void LicenseAgreement::valueChanged(int value)
{
	if(value == m_pScrollBar->maximum())
		ui->agree->show();
	else
		ui->agree->hide();
}

void LicenseAgreement::on_proceed_clicked()
{
	m_userAgreed = true;
	close();
}
#endif